package org.maylove.wuziqi.controller;

import org.maylove.wuziqi.common.RedisUtil;
import org.maylove.wuziqi.common.ResposeMessage;
import org.maylove.wuziqi.pojo.notice.Notice;
import org.maylove.wuziqi.pojo.notice.dto.AddNoticeDto;
import org.maylove.wuziqi.pojo.notice.dto.GetNoticeDto;
import org.maylove.wuziqi.pojo.notice.dto.ModifyNoticeDto;
import org.maylove.wuziqi.service.NoticeService;
import org.maylove.wuziqi.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtil redisUtil;

    Logger logger = LoggerFactory.getLogger(getClass());

    //  获取前三条公告
    @PostMapping("/getMainNotice")
    @ResponseBody
    public ResposeMessage<List<Notice> > getMainNotice() {
        List<Notice> notices;
        //  找一下redis里面有没有
        if(redisUtil.exists("notices")) {
            notices = (List<Notice>)redisUtil.get("notices");
        }
        else {
            notices = noticeService.getMainNotice();
            redisUtil.set("notices", notices);
        }

        return ResposeMessage.success("success", notices);
    }

    //  在每页有pageNum条公告的情况下，获取第pagePos页的公告
    @PostMapping("/getAllNotice")
    @ResponseBody
    public ResposeMessage<List<Notice>> getAllNotice(@RequestBody GetNoticeDto getNoticeDto) {
        List<Notice> notices=noticeService.getAllNotice(getNoticeDto);
        return ResposeMessage.success("success", notices);
    }

    //  发布公告
    @PostMapping("/addNotice")
    @ResponseBody
    public ResposeMessage addNotice(@RequestHeader("token") String token, @RequestBody AddNoticeDto addNoticeDto) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        //  查找用户是否有权限
        if(userService.getIdentity((Long)redisUtil.get(token + "_userId")) < 1) {
            return ResposeMessage.success("Insufficient user permissions!", null);
        }

        Notice notice=new Notice();
        notice.setTitle(addNoticeDto.getTitle());
        notice.setContent(addNoticeDto.getContent());
        notice.setUserId((Long)redisUtil.get(token + "_userId"));

        noticeService.addNotice(notice);
        if(redisUtil.exists("notices")) redisUtil.delete("notices");
        logger.info("Player id:{} add notice:{} success", ((Long) redisUtil.get(token + "_userId")), addNoticeDto.getTitle());
        return ResposeMessage.success("success", null);
    }

    //  删除公告
    @PostMapping("/deleteNotice")
    @ResponseBody
    public ResposeMessage deleteNotice(@RequestHeader("token") String token, @RequestBody Long noticeId) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        //  查找用户是否有权限
        int identity = userService.getIdentity((Long)redisUtil.get(token + "_userId"));
        if(identity < 1 || (identity == 1 && !noticeService.getUserId(noticeId).equals((Long)redisUtil.get(token + "_userId")))) {
            return ResposeMessage.success("Insufficient user permissions!", null);
        }

        noticeService.deleteNotice(noticeId);
        if(redisUtil.exists("notices")) redisUtil.delete("notices");
        logger.info("Player id:{} delete notice id:{} success", ((Long) redisUtil.get(token + "_userId")), noticeId);
        return ResposeMessage.success("success", null);
    }

    //  修改公告
    @PostMapping("/modifyNotice")
    @ResponseBody
    public ResposeMessage modeifyNotice(@RequestHeader("token") String token, @RequestBody ModifyNoticeDto modifyNoticeDto) {
        //  查找一下用户是否处于登录态
        if(token.isEmpty() || !redisUtil.exists(token + "_userId") || redisUtil.get(token + "_userId") == null || !redisUtil.get(redisUtil.get(token + "_userId").toString()).equals(token)) {
            return ResposeMessage.success("User not logged in!", null);
        }

        //  查找用户是否有权限
        int identity = userService.getIdentity((Long)redisUtil.get(token + "_userId"));
        if(!noticeService.getUserId(modifyNoticeDto.getNoticeId()).equals((Long)redisUtil.get(token + "_userId"))) {
            return ResposeMessage.success("Insufficient user permissions!", null);
        }

        noticeService.deleteNotice(modifyNoticeDto.getNoticeId());
        Notice notice = new Notice();
        notice.setTitle(modifyNoticeDto.getTitle());
        notice.setContent(modifyNoticeDto.getContent());
        notice.setUserId((Long)redisUtil.get(token + "_userId"));
        noticeService.addNotice(notice);
        if(redisUtil.exists("notices")) redisUtil.delete("notices");
        logger.info("Player id:{} change notice:{} success", ((Long) redisUtil.get(token + "_userId")), modifyNoticeDto.getTitle());
        return ResposeMessage.success("success", null);
    }
}
